#include "CApp.h"
#include <iostream>
#include <unistd.h>

using namespace std;

CApp::CApp() {
	Surf_Display = NULL;
	Surf_Grid = NULL;
	Surf_X = NULL;
	Surf_O = NULL;

	Renderer = NULL;
	Texture = NULL;

	Running = true;
}
 
int CApp::OnExecute() {
	if(OnInit() == false)
		return -1;

	SDL_Event Event;

	while(Running) {
		while(SDL_PollEvent(&Event)) {
			OnEvent(&Event);
		}

		OnLoop();

		OnRender();
		usleep(10000);
	}

	OnCleanup();

    return 0;
}

bool CApp::OnInit() {
	if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
	{
		return false;
	}

	Surf_Display = SDL_CreateWindow("Game Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
		600, 600, SDL_WINDOW_RESIZABLE  | SDL_WINDOW_OPENGL);

	if(Surf_Display == NULL) {
		return false;
	}

	Renderer = SDL_CreateRenderer(Surf_Display, -1, SDL_RENDERER_ACCELERATED);

	if(Renderer == NULL) {
		return false;
	}

	if((Surf_Grid = SDL_LoadBMP("../grid.bmp")) == NULL) {
		return false;
	}

	if((Surf_X = SDL_LoadBMP("../x.bmp")) == NULL) {
		return false;
	}

	if((Surf_O = SDL_LoadBMP("../o.bmp")) == NULL) {
		return false;
	}

	SDL_SetColorKey(Surf_X, SDL_RLEACCEL, SDL_MapRGB(Surf_X->format, 255, 0, 255));

	SDL_SetColorKey(Surf_O, SDL_RLEACCEL, SDL_MapRGB(Surf_O->format, 255, 0, 255));

	Texture = SDL_CreateTextureFromSurface(Renderer, Surf_Grid);

	Reset();

	return true;
}

void CApp::OnEvent(SDL_Event* Event) {
	CEvent::OnEvent(Event);
}

void CApp::OnLoop() {

}

void CApp::OnRender() {
	// CSurface::OnDraw(SDL_GetWindowSurface(Surf_Display), Surf_Test, 0, 0);

	SDL_RenderClear(Renderer);

	for(int i = 0;i < 9;i++) {
	    int X = (i % 3) * 200;
	    int Y = (i / 3) * 200;

	    SDL_Rect DestR;
	    DestR.x = X; DestR.y = Y;
	 
	    if(Grid[i] == GRID_TYPE_X) {
	        bool r = SDL_BlitSurface(Surf_X, NULL, Surf_Grid, &DestR);
	        // cout << "r " << r;
	    }else if(Grid[i] == GRID_TYPE_O) {
	        SDL_BlitSurface(Surf_O, NULL, Surf_Grid, &DestR);
	    }
	}

	if(Texture) {
		SDL_DestroyTexture(Texture);
	}

	Texture = SDL_CreateTextureFromSurface(Renderer, Surf_Grid);

	SDL_RenderCopy(Renderer, Texture, NULL, NULL);


	SDL_RenderPresent(Renderer);
}

void CApp::OnCleanup() {
	if(Surf_Grid) {
		SDL_FreeSurface(Surf_Grid);
	}

	if(Surf_X) {
		SDL_FreeSurface(Surf_X);
	}

	if(Surf_O) {
		SDL_FreeSurface(Surf_O);
	}

	if(Texture) {
		SDL_DestroyTexture(Texture);
	}

	if(Renderer) {
		SDL_DestroyRenderer(Renderer);
	}

	if(Surf_Display) {
		SDL_DestroyWindow(Surf_Display);
	}

	SDL_Quit();
}

void CApp::OnExit() {
	Running = false;
}

void CApp::Reset() {
	for(int i = 0;i < 9;i++) {
        Grid[i] = GRID_TYPE_NONE;
    }
    CurrentPlayer = 0;
}

void CApp::SetCell(int ID, int Type) {
    if(ID < 0 || ID >= 9) return;
    if(Type < 0 || Type > GRID_TYPE_O) return;
 
    Grid[ID] = Type;
}

void CApp::OnLButtonDown(int mX, int mY) {
    int ID    = mX / 200;
    ID = ID + ((mY / 200) * 3);
 
    if(Grid[ID] != GRID_TYPE_NONE) {
        return;
    }
 
    if(CurrentPlayer == 0) {
        SetCell(ID, GRID_TYPE_X);
        CurrentPlayer = 1;
    }else{
        SetCell(ID, GRID_TYPE_O);
        CurrentPlayer = 0;
    }
}